<?php
// +----------------------------------------------------------------------
// | 数据回收管理
// +----------------------------------------------------------------------
// | @link ( http://www.yurnero.net )
// +----------------------------------------------------------------------
// | @copyright
// +----------------------------------------------------------------------
// | @licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | @author Haijun Wu <nicholasinlove@126.com>
// +----------------------------------------------------------------------
// | $Id: TrashAction.class.php 41 2011-02-22 10:38:36Z nicholasinlove1986@gmail.com $
// +----------------------------------------------------------------------


class TrashAction extends BaseAction {

	public function index() {
		redirect(__APP__);
	}
	
	public function trashLinks() {
		//排序字段 默认为主键
		if (isset($_REQUEST ['order'])) {
			$order = $_REQUEST ['order'];
		} else {
			$order = 'link_id';
		}
		//排序方式 默认按照倒序排列
		if (isset($_REQUEST ['sort'])) {
			$sort = $_REQUEST ['sort'];
		} else {
			$sort = 'desc';
		}
		//查询过滤
		$map  =	 array();
		$map['link_status'] = 0;
		if ($_REQUEST['keyword']!='') {
			$map['link_name'] = array('like',"%{$_REQUEST['keyword']}%");
		}
		//列表
		$links  =  M ('Links');
		import("@.ORG.Page");
		C ( 'PAGE_ROLLPAGE',C('page_roll') );
		$count = $links->where($map)->count();
		$p = new Page ($count, C('admin_list_num'));
		$list = $links->where($map)->order("`".$order."` ".$sort)->limit($p->firstRow.','.$p->listRows)->findAll();
		$p->setConfig('theme',C('admin_page_theme'));
		$page = $p->show ();
		
		$this->assign('action_link', array('href'=>__APP__.'/Links/listLinks/', 'text' => '链接列表'));
		$this->assign('ur_here','链接回收');
		$this->assign('pagestyle',C('admin_page_style'));
		$this->assign('page', $page);
		$this->assign('list',$list);
		$this->assign('sort',$sort);
		$this->display();
	}
	
	public function handleLinks() {
		$this->checkLimits();
		$links  =  M('Links');
		$select =  isset($_POST['select']) ? $_POST['select'] : '';
		/* 批量操作 */
		if ($select) {	
			if ($_POST['act'] == 'restore') {
				$pk   =  $links->getPk();
				$condition = array ($pk => array ('in', $_POST['key']));
				$list = $links->where($condition)->setField('link_status', 1);
				if ($list!==false) {
					clear_cache_files();    // 清除缓存
					set_log('restore(Links, Count: '.count($_POST['key']).')');
					$this->assign('jumpUrl',__URL__.'/trashLinks/');
					$this->success ('成功还原了 '.count($_POST['key']).' 个链接' );
				} else {				
					$this->error ('操作失败');
				}
			}
			if ($_POST['act'] == 'drop') {
				$pk   =  $links->getPk();
				$condition = array ($pk => array ('in', $_POST['key']));
				$list = $links->where($condition)->delete();
				if ($list!==false) {
					//clear_cache_files();    // 清除缓存
					set_log('drop(Links, Count: '.count($_POST['key']).')');
					$this->assign('jumpUrl',__URL__.'/trashLinks/');
					$this->success ('成功删除了 '.count($_POST['key']).' 个链接' );
				} else {
					$this->error ('操作失败');
				}
			}			
		}else {	
			$map	=	array();	
			if ($_REQUEST['act'] == 'restore') {
				$map['link_id']	 =	$_REQUEST['id'];
				$list = $links->where($map)->setField('link_status', 1);
				if ($list!==false) {
					clear_cache_files();    // 清除缓存
					set_log('restore(Links, Id: '.$_REQUEST['id'].')');
					$this->assign('jumpUrl',__URL__.'/trashLinks/');
					$this->success('操作成功');
				} else {
					$this->error ('操作失败');
				}
			}
			if ($_REQUEST['act'] == 'drop') {
				$map['link_id']	 =	$_REQUEST['id'];
				$list = $links->where($map)->delete();
				if ($list!==false) {
					//clear_cache_files();    // 清除缓存
					set_log('drop(Links, Id: '.$_REQUEST['id'].')');
					$this->assign('jumpUrl',__URL__.'/trashLinks/');
					$this->success('操作成功');
				} else {
					$this->error ('操作失败');
				}
			}	
		}	
	}
	
	public function trashPosts() {
		//排序字段 默认为主键
		if (isset($_REQUEST ['order'])) {
			$order = $_REQUEST ['order'];
		} else {
			$order = 'posts_id';
		}
		//排序方式 默认按照倒序排列
		if (isset($_REQUEST ['sort'])) {
			$sort = $_REQUEST ['sort'];
		} else {
			$sort = 'desc';
		}
		//查询过滤			
		$map  =	 array();		
		$map['posts_status'] = 0;
		$map['posts_type']   = 1;
		if ($_REQUEST['keyword']!='') {
			$map['posts_title'] = array('like',"%{$_REQUEST['keyword']}%");
		}	
		//列表
		$posts  =  M ('Posts');
		//$cat    =  D('Category');
		//$user   =  D('AdminUser');
		//$tag    =  D('Tag');
		//$cat_option  =  $cat->getCatList(0,0,0,true);
		//$nick_name   =  $user->getUserNickName(true);
		//$tag_name    =  $tag->getTagName(true);
		
		import("@.ORG.Page");
		C ( 'PAGE_ROLLPAGE',C('page_roll') );	
		$count = $posts->where($map)->count();
		$p = new Page ($count, C('admin_list_num'));
		$list = $posts->where($map)->order("`".$order."` ".$sort)->limit($p->firstRow.','.$p->listRows)->findAll();
		$p->setConfig('theme',C('admin_page_theme'));
		//分页跳转的时候保证查询条件
		foreach ($map as $key => $val) {
			if (!is_array ($val)) {
				$p->parameter .= "$key=" . urlencode ( $val ) . "&";
			}
		}
		//dump($list);
		$page = $p->show ();

		$sort = $sort.'_'.$order;
		
		$this->assign('action_link', array('href'=>__APP__.'/Posts/listPosts/', 'text' => '日志列表'));
		$this->assign('ur_here','日志回收');
		$this->assign('pagestyle',C('admin_page_style'));
		$this->assign('page', $page);
		$this->assign('list',$list);
		$this->assign('sort',$sort);
		$this->display();
	}
	
	public function handlePosts() {
		$this->checkLimits();
		$posts  =  M('Posts');
		$select =  isset($_POST['select']) ? $_POST['select'] : '';
		/* 批量操作 */
		if ($select) {	
			if ($_POST['act'] == 'restore') {
				$pk   =  $posts->getPk();
				$condition = array ($pk => array ('in', $_POST['key']));
				$list = $posts->where($condition)->setField('posts_status', 1);
				if ($list!==false) {
					clear_cache_files();    // 清除缓存
					set_log('restore(Posts, Count: '.count($_POST['key']).')');
					$this->assign('jumpUrl',__URL__.'/trashPosts/');
					$this->success ('成功还原了 '.count($_POST['key']).' 个日志' );
				} else {
					$this->error ('操作失败');
				}
			}
			if ($_POST['act'] == 'drop') {
				$pk   =  $posts->getPk();
				$condition = array ($pk => array ('in', $_POST['key']));
				$list = $posts->where($condition)->delete();
				if ($list!==false) {
					/*这里还需要处理评论,标签*/
					//clear_cache_files();    // 清除缓存
					set_log('drop(Posts, Count: '.count($_POST['key']).')');
					$this->assign('jumpUrl',__URL__.'/trashPosts/');
					$this->success ('成功删除了 '.count($_POST['key']).' 个链接' );
				} else {
					$this->error ('操作失败');
				}
			}			
		}else {	
			$map	=	array();	
			if ($_REQUEST['act'] == 'restore') {
				$map['posts_id']	 =	$_REQUEST['id'];
				$list = $posts->where($map)->setField('posts_status', 1);
				if ($list!==false) {
					clear_cache_files();    // 清除缓存
					set_log('restore(Posts, Id: '.$_REQUEST['id'].')');
					$this->assign('jumpUrl',__URL__.'/trashPosts/');
					$this->success('操作成功');
				} else {
					$this->error ('操作失败');
				}
			}
			if ($_REQUEST['act'] == 'drop') {
				$map['posts_id']	 =	$_REQUEST['id'];
				$list = $posts->where($map)->delete();
				if ($list!==false) {
					/*这里还需要处理评论,标签*/
					//clear_cache_files();    // 清除缓存
					set_log('drop(Posts, Id: '.$_REQUEST['id'].')');
					$this->assign('jumpUrl',__URL__.'/trashPosts/');
					$this->success('操作成功');
				} else {
					$this->error ('操作失败');
				}
			}	
		}	
	}

	public function trashComment() {
		//排序字段 默认为主键
		if (isset($_REQUEST ['order'])) {
			$order = $_REQUEST ['order'];
		} else {
			$order = 'comment_id';
		}
		//排序方式 默认按照倒序排列
		if (isset($_REQUEST ['sort'])) {
			$sort = $_REQUEST ['sort'];
		} else {
			$sort = 'desc';
		}
		//查询
		$map  =	 array();	
		$map['comment_status'] = 0;	
		if ($_REQUEST['keyword']!='') {
			$map['comment_content'] = array('like',"%{$_REQUEST['keyword']}%");
		}
		//列表
		$com  =  M('Comment');	
		import("@.ORG.Page");
		C ( 'PAGE_ROLLPAGE',C('page_roll') );
		$count = $com->where($map)->count();
		$p = new Page ($count, C('admin_list_num'));
		$list = $com->where($map)->order("`".$order."` ".$sort)->limit($p->firstRow.','.$p->listRows)->findAll();
		$p->setConfig('theme',C('admin_page_theme'));
		$page = $p->show ();
		
		$this->assign('action_link', array('href'=>__APP__.'/Comment/listComment/', 'text' => '评论管理'));
		$this->assign('ur_here','评论回收');
		$this->assign('pagestyle',C('admin_page_style'));	
		$this->assign('page', $page);
		$this->assign('list',$list);
		$this->assign('sort',$sort);
		$this->display();
	}
	
	public function handleComment() {
		$this->checkLimits();
		$com  =  M('Comment');
		$select =  isset($_POST['select']) ? $_POST['select'] : '';
		/* 批量操作 */
		if ($select) {	
			if ($_POST['act'] == 'restore') {
				$pk   =  $com->getPk();
				$condition = array ($pk => array ('in', $_POST['key']));
				$list = $com->where($condition)->setField('comment_status', 1);
				if ($list!==false) {
					clear_cache_files();    // 清除缓存
					set_log('restore(Comment, Count: '.count($_POST['key']).')');
					$this->assign('jumpUrl',__URL__.'/trashComment/');
					$this->success ('成功还原了 '.count($_POST['key']).' 条评论' );
				} else {
					$this->error ('操作失败');
				}
			}
			if ($_POST['act'] == 'drop') {
				$pk   =  $com->getPk();
				$condition = array ($pk => array ('in', $_POST['key']));
				$list = $com->where($condition)->delete();
				if ($list!==false) {
					clear_cache_files();    // 清除缓存
					set_log('drop(Comment, Count: '.count($_POST['key']).')');
					$this->assign('jumpUrl',__URL__.'/trashComment/');
					$this->success ('成功删除了 '.count($_POST['key']).' 条评论' );
				} else {
					$this->error ('操作失败');
				}
			}			
		}else {	
			$map	=	array();	
			if ($_REQUEST['act'] == 'restore') {
				$map['comment_id']	 =	$_REQUEST['id'];
				$list = $com->where($map)->setField('comment_status', 1);
				if ($list!==false) {
					clear_cache_files();    // 清除缓存
					set_log('restore(Comment, Id: '.$_REQUEST['id'].')');
					$this->assign('jumpUrl',__URL__.'/trashComment/');
					$this->success('操作成功');
				} else {
					$this->error ('操作失败');
				}
			}
			if ($_REQUEST['act'] == 'drop') {
				$map['comment_id']	 =	$_REQUEST['id'];
				$list = $com->where($map)->delete();
				if ($list!==false) {
					clear_cache_files();    // 清除缓存
					set_log('drop(Comment, Id: '.$_REQUEST['id'].')');
					$this->assign('jumpUrl',__URL__.'/trashComment/');
					$this->success('操作成功');
				} else {
					$this->error ('操作失败');
				}
			}	
		}	
	}
		
}

?>
